package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Company;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 公司Dao
 * 作者 : zdl
 * 日期 : 2017/6/28 13:45
 */
@Repository
public class CompanyDao extends BaseDao {

    public void saveCompany(Company company) {
        super.save(company);
    }

    public void deleteCompany(Company company) {
        super.delete(company);
    }

    public void updateCompany(Company company) {
        super.update(company);
    }

    public void saveOrUpdateCompany(Company company) {
        super.saveOrUpdate(company);
    }

    public Company getCompanyById(String id) {
        return (Company) super.get(Company.class, id);
    }

    public List<Company> listAllCompany() {
        String hql = "FROM Company WHERE isDelete=0";
        return super.find(hql);
    }

    /**
     * 分页查询公司列表
     *
     * @param page    分页对象
     * @param company 公司查询
     * @return page
     */
    public Page<Company> pageByCompany(Page<Company> page, Company company) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Company c WHERE c.isDelete = 0 ");
        if (company != null) {
            Map<String, Object> param = new HashMap<>();
            String companyName = company.getName();
            if (StringUtils.isNotBlank(companyName)) {
                hqlSb.append("AND c.name LIKE ?1 ");
                param.put("1", "%" + companyName + "%");
            }
            hqlSb.append("ORDER BY c.createTime DESC");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    public Company getCompanyByName(String name) {
        String hql = "FROM Company WHERE isDelete = 0 AND name = '" + name + "'";
        return (Company) super.get(hql);
    }

    public Company getLastCompany() {
        String hql = "FROM Company WHERE isDelete = 0 ORDER BY createTime DESC LIMIT 0,1";
        return (Company) super.get(hql);
    }

    /**
     * 根据企业代码查询企业信息
     *
     * @param comCode 企业代码
     * @return 公司
     */
    public Company getCompanyByComCode(String comCode) {
        String hql = "FROM Company WHERE isDelete = 0 AND comCode = '" + comCode + "'";
        return (Company) super.get(hql);
    }
}
